package com.umeng.soexample.mvp.view.fragment;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.lib_core.base.BaseFragment;
import com.umeng.soexample.R;
import com.umeng.soexample.ali_demo.PayDemoActivity;
import com.umeng.soexample.bean.AddressListEntity;
import com.umeng.soexample.bean.CarListEntity;
import com.umeng.soexample.bean.CouponListEntity;
import com.umeng.soexample.dao.USerService;
import com.umeng.soexample.di.compont.DaggerAffirmOrderFragmentCompont;
import com.umeng.soexample.di.modules.AffirmOrderFragmentModules;
import com.umeng.soexample.mvc.control.SPHelperSave;
import com.umeng.soexample.mvp.contract.AffirmOrderFragmentContract;
import com.umeng.soexample.mvp.persenter.AffirmOrderFragmentPersenter;
import com.umeng.soexample.mvp.view.adapter.AffirmOrderAdapter;

import java.util.ArrayList;
import java.util.List;

public class AffirmOrderFragment extends BaseFragment<AffirmOrderFragmentPersenter> implements AffirmOrderFragmentContract.AffirmOrderFragmentIView,View.OnClickListener{


    private String user_id;
    private String address_id;//默认地址id
    private List<String> goods_ids = new ArrayList<>();
    private View view;
    //TODO 地址
    private TextView tv_affirm_order_user_name;
    private TextView tv_affirm_order_address_tag;
    private TextView tv_affirm_order_phone;
    private TextView tv_affirm_order_address_area;
    private TextView tv_affirm_order_address_detail;
    //TODO 支付方式
    private TextView tv_affirm_order_pay_type;
    private ImageView iv_affirm_order_pay_type;
    private LinearLayout ll_affirm_order_pay_type;
    private boolean xs_pay_type = false;
    private RadioButton rb_affirm_order_online_pay;
    private RadioButton rb_affirm_order_arrive_pay;
    private RadioGroup rg_affirm_order_pay_type;
    //TODO 配送方式
    private TextView tv_affirm_order_express_type;
    private ImageView iv_affirm_order_express_type;
    private LinearLayout ll_affirm_order_express_type;
    private boolean xs_express_type = false;
    private RadioButton rb_affirm_order_common_express;
    private RadioButton rb_affirm_order_sf_express;
    private RadioGroup rg_affirm_order_express_type;
    private double expressPrice;//运费
    //TODO 商品列表
    private RecyclerView rv_affirm_order;
    private AffirmOrderAdapter adapter;
    //TODO 优惠券
    private TextView tv_affirm_order_yhqnum;
    private int yhqnum;//优惠券数量
    private double yhqPrice;//优惠金额
    //TODO 有货币 发票 不打印价格
    private TextView tv_affirm_order_yhbnum;
    private CheckBox cb_affirm_order_yhb;
    private CheckBox cb_affirm_order_invoice;
    private CheckBox cb_affirm_order_no_print_price;
    //TODO 商品总金额 运费活动金额 优惠券
    private TextView tv_affirm_order_sumprice;
    private double sumprice;
    private TextView tv_affirm_order_freight;
    private TextView tv_affirm_order_activity_price;
    private TextView tv_affirm_order_yhq;
    //TODO 悬框
    private TextView tv_affirm_order_send_to;
    private RelativeLayout rl_affirm_order_send_to;
    //TODO 实付金额 确认按钮
    private TextView tv_affirm_order_bottom_sumprice;
    private Button btn_affirm_order_affirm;
    @Override
    public View createView(LayoutInflater inflater) {
        view = inflater.inflate(R.layout.fragment_affirm_order, null);
        return view;
    }

    @Override
    public void init(View view) {
        //TODO 地址
        tv_affirm_order_user_name = view.findViewById(R.id.tv_affirm_order_user_name);//收货人姓名
        tv_affirm_order_address_tag = view.findViewById(R.id.tv_affirm_order_address_tag);//标签
        tv_affirm_order_phone = view.findViewById(R.id.tv_affirm_order_phone);//手机号
        tv_affirm_order_address_area = view.findViewById(R.id.tv_affirm_order_address_area);//大致地址
        tv_affirm_order_address_detail = view.findViewById(R.id.tv_affirm_order_address_detail);//详细地址
        //TODO 支付方式
        tv_affirm_order_pay_type = view.findViewById(R.id.tv_affirm_order_pay_type);//显示支付方式
        iv_affirm_order_pay_type = view.findViewById(R.id.iv_affirm_order_pay_type);//支付方式右侧箭头
        rb_affirm_order_online_pay = view.findViewById(R.id.rb_affirm_order_online_pay);//在线支付
        rb_affirm_order_arrive_pay = view.findViewById(R.id.rb_affirm_order_arrive_pay);//货到付款
        rg_affirm_order_pay_type = view.findViewById(R.id.rg_affirm_order_pay_type);//支付方式选择按钮组
        //TODO 配送方式
        tv_affirm_order_express_type = view.findViewById(R.id.tv_affirm_order_express_type);//显示配送方式
        iv_affirm_order_express_type = view.findViewById(R.id.iv_affirm_order_express_type);//配送方式右侧箭头
        rb_affirm_order_common_express = view.findViewById(R.id.rb_affirm_order_common_express);//普通
        rb_affirm_order_sf_express = view.findViewById(R.id.rb_affirm_order_sf_express);//顺丰
        rg_affirm_order_express_type = view.findViewById(R.id.rg_affirm_order_express_type);//配送方式选择按钮组
        //TODO 商品列表
        rv_affirm_order = view.findViewById(R.id.rv_affirm_order);
        adapter = new AffirmOrderAdapter(R.layout.item_affirm_order);
        rv_affirm_order.setAdapter(adapter);
        rv_affirm_order.setLayoutManager(new LinearLayoutManager(getContext()));
        //TODO 优惠券
        tv_affirm_order_yhqnum = view.findViewById(R.id.tv_affirm_order_yhqnum);
        //TODO 有货币 发票 不打印价格
        tv_affirm_order_yhbnum = view.findViewById(R.id.tv_affirm_order_yhbnum);
        cb_affirm_order_yhb = view.findViewById(R.id.cb_affirm_order_yhb);
        cb_affirm_order_invoice = view.findViewById(R.id.cb_affirm_order_invoice);
        cb_affirm_order_no_print_price = view.findViewById(R.id.cb_affirm_order_no_print_price);
        //TODO 商品总金额 运费活动金额 优惠券
        tv_affirm_order_sumprice = view.findViewById(R.id.tv_affirm_order_sumprice);
        tv_affirm_order_freight = view.findViewById(R.id.tv_affirm_order_freight);
        tv_affirm_order_activity_price = view.findViewById(R.id.tv_affirm_order_activity_price);
        tv_affirm_order_yhq = view.findViewById(R.id.tv_affirm_order_yhq);
        //TODO 悬框
        tv_affirm_order_send_to = view.findViewById(R.id.tv_affirm_order_send_to);
        rl_affirm_order_send_to = view.findViewById(R.id.rl_affirm_order_send_to);
        //TODO 实付金额
        tv_affirm_order_bottom_sumprice = view.findViewById(R.id.tv_affirm_order_bottom_sumprice);
    }

    @Override
    public void initData() {
        Bundle affirm_order = getArguments();
        goods_ids=affirm_order.getStringArrayList("goods_ids");

        Cursor user = USerService.getInstance().query("user", null, null);
        if (user.moveToNext()){
             user_id = user.getString(user.getColumnIndex("user_id"));
             p.addressListRequest(user_id);
             p.couponListRequest(user_id);
             p.carListRequest(user_id);
        }
    }

    @Override
    public void setupDagger() {
        DaggerAffirmOrderFragmentCompont.builder().affirmOrderFragmentModules(new AffirmOrderFragmentModules(this))
                .build().inject(this);
    }

    @Override
    public void initListener() {
        view.findViewById(R.id.tv_affirm_order_right_red).setOnClickListener(this);//其他地址
        view.findViewById(R.id.ll_affirm_order_pay_type).setOnClickListener(this);//支付方式整体点击
        view.findViewById(R.id.ll_affirm_order_express_type).setOnClickListener(this);//配送方式整体点击
        view.findViewById(R.id.btn_affirm_order_affirm).setOnClickListener(this);//确认按钮

        //支付方式选择
        rg_affirm_order_pay_type.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb_affirm_order_online_pay://在线支付
                        tv_affirm_order_pay_type.setText(rb_affirm_order_online_pay.getText());
                        break;
                    case R.id.rb_affirm_order_arrive_pay://到付
                        tv_affirm_order_pay_type.setText(rb_affirm_order_arrive_pay.getText());
                        break;
                }
            }
        });
        rg_affirm_order_pay_type.check(R.id.rb_affirm_order_online_pay);
        //配送方式选择
        rg_affirm_order_express_type.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb_affirm_order_common_express://普通快递
                        tv_affirm_order_express_type.setText("普通快递:运费￥0.00");
                        expressPrice = 0;
                        break;
                    case R.id.rb_affirm_order_sf_express://顺丰快递
                        tv_affirm_order_express_type.setText("顺丰速递:运费￥5.00");
                        expressPrice = 5.00;
                        break;
                }
                tv_affirm_order_freight.setText("+￥" + expressPrice );
                tv_affirm_order_bottom_sumprice.setText("￥" + (sumprice + expressPrice));
            }
        });
        rg_affirm_order_express_type.check(R.id.rb_affirm_order_common_express);
    }

    @Override
    public void addressListSuccess(AddressListEntity entity) {
        if ("1".equals(entity.statues)){
            address_id = SPHelperSave.getInstance().getAddress_default();
            for (AddressListEntity.ValuesBean valuesBean : entity.values){
                if (address_id.equals(valuesBean.address_id)){
                    tv_affirm_order_user_name.setText("*" + valuesBean.user_name.substring(1));
                    tv_affirm_order_address_tag.setText("默认");
                    tv_affirm_order_phone.setText(valuesBean.phone.substring(0,3) + "****" + valuesBean.phone.substring(7));
                    tv_affirm_order_address_area.setText(valuesBean.address_area);
                    tv_affirm_order_address_detail.setText(" " + valuesBean.address_detail);
                    tv_affirm_order_send_to.setText(valuesBean.address_area + " " + valuesBean.address_detail);
                    break;
                }
            }
        }
    }

    /**
     * 优惠券返回接口
     *
     * @param entity
     */
    @Override
    public void couponListSuccess(CouponListEntity entity) {
        if ("1".equals(entity.statues)){
            yhqnum = entity.values.size();
            yhqPrice=0;
            tv_affirm_order_yhqnum.setText(yhqnum + "张可用 已推荐0张");
            tv_affirm_order_yhq.setText("-￥" + yhqPrice);
        }
    }

    /**
     * 购物车返回接口
     *
     * @param entity
     */
    @Override
    public void carListSuccess(CarListEntity entity) {
//        if ("1".equals(entity.statues)){
//
//            //合并
//
//            ArrayList<CarListEntity.ValuesBean> heBean = new ArrayList<>();
//            for (CarListEntity.ValuesBean bean:heBean
//                 ) {
//                boolean you=true;
//
//                for (CarListEntity.ValuesBean neibean:heBean
//                     ) {
//                    if (neibean.goods_id.equals(bean.goods_id)){
//                        neibean.shop_num = Integer.parseInt(neibean.shop_num) + Integer.parseInt(bean.shop_num) + "";
//                        you=false;
//                        break;
//                    }
//                }
//                if (you){
//                    heBean.add(bean);
//                }
//            }

        if ("1".equals(entity.statues)) {

            //合并
            List<CarListEntity.ValuesBean> heBean = new ArrayList<>();
            for (CarListEntity.ValuesBean bean : entity.values) {
                boolean you = true;
                for (CarListEntity.ValuesBean neibean : heBean) {
                    if (neibean.goods_id.equals(bean.goods_id)) {
                        neibean.shop_num = Integer.parseInt(neibean.shop_num) + Integer.parseInt(bean.shop_num) + "";
                        you = false;
                        break;
                    }
                }
                if (you) {
                    heBean.add(bean);
                }
            }

//            sumprice=0;
//            //取出选中的商品
//            List<CarListEntity.ValuesBean> beans = new ArrayList<>();
//            for (CarListEntity.ValuesBean bean:heBean
//                 ) {
//                for (String id:goods_ids
//                     ) {
//                    if (id.equals(bean.goods_id)){
//                        sumprice += (Integer.parseInt(bean.shop_num) * Double.parseDouble(bean.shop_price));
//                        break;
//                    }
//                }
//            }
//
//            adapter.setNewData(beans);
//            tv_affirm_order_sumprice.setText("￥" + sumprice);
//            tv_affirm_order_bottom_sumprice.setText("￥" + (sumprice + expressPrice));

            sumprice = 0;
            //取出选中的商品
            List<CarListEntity.ValuesBean> beans = new ArrayList<>();
            for (CarListEntity.ValuesBean bean : heBean) {
                for (String id : goods_ids) {
                    if (id.equals(bean.goods_id)) {
                        beans.add(bean);
                        sumprice += (Integer.parseInt(bean.shop_num) * Double.parseDouble(bean.shop_price));
                        break;
                    }
                }
            }

            adapter.setNewData(beans);
            tv_affirm_order_sumprice.setText("￥" + sumprice);
            tv_affirm_order_bottom_sumprice.setText("￥" + (sumprice + expressPrice));

        }
    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_affirm_order_back://返回上一页
                getActivity().finish();
                break;
            case R.id.tv_affirm_order_right_red://其他地址
                break;
            case R.id.ll_affirm_order_pay_type://支付方式整体点击
                xs_pay_type = !xs_pay_type;
                if (xs_pay_type){
                    //关闭配送选择
                    rg_affirm_order_express_type.setVisibility(View.GONE);
                    //显示支付选择
                    rg_affirm_order_pay_type.setVisibility(View.VISIBLE);
                    iv_affirm_order_pay_type.setImageResource(R.drawable.coupon_tab_unselected_up);
                }else{
                    rg_affirm_order_pay_type.setVisibility(View.GONE);
                    iv_affirm_order_pay_type.setImageResource(R.drawable.coupon_tab_unselected_down);
                }
                break;
            case R.id.ll_affirm_order_express_type://配送方式整体点击
                xs_express_type = !xs_express_type;
                if (xs_express_type){
                    //关闭支付选择
                    rg_affirm_order_pay_type.setVisibility(View.GONE);
                    //显示配送选择
                    rg_affirm_order_express_type.setVisibility(View.VISIBLE);
                    iv_affirm_order_express_type.setImageResource(R.drawable.coupon_tab_unselected_up);
                }else{
                    rg_affirm_order_express_type.setVisibility(View.GONE);
                    iv_affirm_order_express_type.setImageResource(R.drawable.coupon_tab_unselected_down);
                }
                break;
            case R.id.btn_affirm_order_affirm://确认按钮
//                Intent alipqy = new Intent(getActivity(), PayDemoActivity.class);
//                alipqy.putExtra("sumprice", "" + (sumprice - expressPrice));
//                getActivity().startActivity(alipqy);
                Intent intent = new Intent(getContext(), PayDemoActivity.class);
                //intent.putExtra("values",price);
               // intent.putExtra("sumprice", "" + (sumprice - expressPrice));
                intent.putExtra("values", "" + (sumprice - expressPrice));
                startActivity(intent);
                break;
        }
    }
}
